package com.lions.client.model;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.lions.client.view.mappanel.MapPanel;
import com.lions.client.view.modulepanel.ModulePanel;
import com.lions.client.view.popup.MyPopupPanel;

public class GPSFeed {
	/**
	 * 
	 * @param feed
	 */
	public GPSFeed(final ModuleList list, final ModulePanel modP,
			final MapPanel mapP) {
		this.values = new String();
		this.responseText = new String();
		this.exists = false;
		modList = list;
		this.modulePanel = modP;
		this.mapPanel = mapP;
	}

	/**
	 * 
	 * @return
	 */
	public boolean isAvailable(final int feed) {
		exists = false;
		this.url = "http://www.d.umn.edu/~abrooks/SomeTests.php?q="
				+ Integer.toString(feed) + ".json?"; // TAKE

		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

		try {
			Request request = builder.sendRequest(null, new RequestCallback() {
				@Override
				public void onResponseReceived(final Request request,
						final Response response) {
					if (200 == response.getStatusCode()) {
						exists = true;

					} else {
						new MyPopupPanel("Error#1",
								"#1 - Couldnt retrieve JSON ("
										+ response.getStatusText() + ")");
					}

				}

				@Override
				public void onError(final Request request,
						final Throwable exception) {
					new MyPopupPanel("Error#2",
							"#2 - onError: Couldnt retrieve JSON");

				}

			});
		} catch (RequestException e) {
			new MyPopupPanel("Error#3", "#3 - Couldnt retrieve JOSN");
		}
		return exists;
	}

	public void update(final int feed) {
		this.url = "http://www.d.umn.edu/~abrooks/SomeTests.php?q="
				+ Integer.toString(feed) + ".json?"; // TAKE

		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

		try {
			Request request = builder.sendRequest(null, new RequestCallback() {
				@Override
				public void onResponseReceived(final Request request,
						final Response response) {
					if (200 == response.getStatusCode()) {
						// responseText = response.getText();
						getModules(response.getText());

					} else {
						new MyPopupPanel("Error#1",
								"#1 - Couldnt retrieve JSON ("
										+ response.getStatusText() + ")");
					}

				}

				@Override
				public void onError(final Request request,
						final Throwable exception) {
					new MyPopupPanel("Error#2",
							"#2 - onError: Couldnt retrieve JSON");

				}

			});
		} catch (RequestException e) {
			new MyPopupPanel("Error#3", "#3 - Couldnt retrieve JOSN");
		}
	}

	/**
	 * 
	 * @return
	 */
	private void getModules(final String rt) {
		this.modList.clear();

		String sAll = rt;
		JSONArray jA = (JSONArray) JSONParser.parseLenient(sAll);

		JSONNumber jN;
		JSONString jS;
		int num;
		String s;

		// AIRLOCK|USABLE|0|5|5|5|false
		for (int i = 0; i < jA.size(); i++) {
			String st = new String();
			JSONObject jO = (JSONObject) jA.get(i);

			// vp.add(new Label(Double.toString(d)));
			jS = (JSONString) jO.get("status");
			s = jS.stringValue();
			jN = (JSONNumber) jO.get("code");
			num = new Double(jN.doubleValue()).intValue();
			int code = num;
			if ((num > 0) && (num < 41)) {
				st += "PLAIN|";
			} else if ((num > 60) && (num < 81)) {
				st += "DORMITORY|";
			} else if ((num > 90) && (num < 101)) {
				st += "SANITATION|";
			} else if ((num > 110) && (num < 121)) {
				st += "FOOD_WATER_STORAGE|";
			} else if ((num > 130) && (num < 135)) {
				st += "GYM|";
			} else if ((num > 140) && (num < 145)) {
				st += "CANTEEN|";
			} else if ((num > 150) && (num < 155)) {
				st += "POWER|";
			} else if ((num > 160) && (num < 165)) {
				st += "CONTROL|";
			} else if ((num > 170) && (num < 175)) {
				st += "AIRLOCK|";
			} else if ((num > 180) && (num < 185)) {
				st += "MEDICAL|";
			} else {
				st += "PLAIN|";
			}

			st += s.toUpperCase();

			st += "|";

			jN = (JSONNumber) jO.get("turns");

			num = new Double(jN.doubleValue()).intValue();
			if (num > -1) {
				st += (new Integer(num).toString()) + "|";
			} else {
				st += (new Integer(0).toString()) + "|";
			}

			if (code > -1) {
				st += (new Integer(code).toString()) + "|";
			} else {
				st += (new Integer(0).toString()) + "|";
			}

			// vp.add(new Label(Double.toString(d)));

			jN = (JSONNumber) jO.get("X");
			num = new Double(jN.doubleValue()).intValue();
			if (num > -1) {
				st += (new Integer(num).toString()) + "|";
			} else {
				st += (new Integer(0).toString()) + "|";
			}

			// vp.add(new Label(Double.toString(d)));
			jN = (JSONNumber) jO.get("Y");
			num = new Double(jN.doubleValue()).intValue();
			if (num > -1) {
				st += new Integer(num).toString();
			} else {
				st += new Integer(0).toString();
			}

			modList.add(new Module(st));
		}
		modulePanel.populate();
		mapPanel.populate();
	}

	private ModulePanel modulePanel;
	private MapPanel mapPanel;
	public ModuleList modList;
	private boolean exists;
	private String values;
	private String url;
	public String responseText;
}